<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [
    <!ENTITY nbsp "&#x00A0;">
]>
<xsl:stylesheet
	version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:msxml="urn:schemas-microsoft-com:xslt"
	xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon"
	xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" xmlns:uForum="urn:uForum"
	xmlns:uForum.raw="urn:uForum.raw" xmlns:uPowers="urn:uPowers"
	exclude-result-prefixes="uPowers uForum.raw msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets uForum ">

    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <xsl:param name="currentPage"/>

    <xsl:template match="/">
        <xsl:variable name="topicID" select="number(umbraco.library:ContextKey('topicID'))"/>
        <xsl:variable name="topic" select="uForum.raw:Topic($topicID)/topics/topic"/>
        <xsl:variable name="maxitems">10</xsl:variable>

        <xsl:variable name="mem">
            <xsl:if test="umbraco.library:IsLoggedOn()">
                <xsl:value-of select="umbraco.library:GetCurrentMember()/@id"/>
            </xsl:if>
        </xsl:variable>

        <xsl:variable name="p">
            <xsl:choose>
                <xsl:when test="string(number( umbraco.library:RequestQueryString('p') )) != 'NaN'">
                    <xsl:value-of select="umbraco.library:RequestQueryString('p')"/>
                </xsl:when>
                <xsl:otherwise>0</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>


        <xsl:variable name="pages" select="uForum:TopicPager($topicID, $maxitems, $p)"/>

        <!-- start the comment table -->
       
        <table class="forumList commentsList" cellspacing="0">
            <tbody>

                <xsl:if test="$p = 0">
                    <tr class="post" id="posts-{$topicID}">
                        <td class="author vcard" style="width: 100px">
                            <xsl:call-template name="badge">
                                <xsl:with-param name="id" select="$topic/memberId" />
                                <xsl:with-param name="date" select="$topic/created" />
                            </xsl:call-template>
                        </td>

                        <td class="body">
                            <xsl:value-of select="umbraco.library:ReplaceLineBreaks( uForum:Sanitize( uForum:ResolveLinks( $topic/body)))" disable-output-escaping="yes"/>
                        </td>
                    </tr>
                </xsl:if>
                <xsl:for-each select="uForum.raw:CommentsByDate($topicID, $maxitems, $p, 'ASC')//comment" >

                    <tr class="spacer">
                        <td colspan="2"></td>
                    </tr>

                    <xsl:call-template name="comment">
                        <xsl:with-param name="comment" select="."/>
                        <xsl:with-param name="topic" select="$topic"/>
                        <xsl:with-param name="mem" select="$mem"/>
                        <xsl:with-param name="collaps" select="false()"/>
                    </xsl:call-template>

                </xsl:for-each>
            </tbody>
        </table>


        <xsl:if test="count($pages//page) &gt; 1">
            <strong>Pages: </strong>
            <ul id="searchPager" class="pager">
                <xsl:for-each select="$pages//page">
                    <li>
                        <xsl:if test="@current = 'true'">
                            <xsl:attribute name="class">current</xsl:attribute>
                        </xsl:if>
                        <a href="?p={@index}">
                            <xsl:value-of select="@index+1"/>
                        </a>
                    </li>
                </xsl:for-each>
            </ul>
        </xsl:if>

    </xsl:template>

    <xsl:template name="comment">
        <xsl:param name="comment"/>
        <xsl:param name="topic"/>
        <xsl:param name="collaps"/>
        <xsl:param name="mem"/>

        <tr class="post postComment" id="comment{$comment/id}">

            <xsl:if test="id = $topic/answer">
                <xsl:attribute name="class">post postComment postSolution</xsl:attribute>
            </xsl:if>

            <xsl:if test="$collaps">
                <xsl:attribute name="class">post postComment hidden</xsl:attribute>
            </xsl:if>


            <td class="author vcard" style="width: 100px">

                <xsl:call-template name="badge">
                    <xsl:with-param name="id" select="$comment/memberId" />
                    <xsl:with-param name="date" select="$comment/created" />
                </xsl:call-template>

                <xsl:if test="id = $topic/answer">
                    <a name="solution" style="visibility: hidden">Solution</a>
                </xsl:if>
                <a name="comment{$comment/id}" style="visibility: hidden">
                    Comment with ID: <xsl:value-of select="$comment/id"/>
                </a>

            </td>
            <td class="body">

                <xsl:value-of select="umbraco.library:ReplaceLineBreaks( uForum:Sanitize( uForum:ResolveLinks($comment/body)))" disable-output-escaping="yes"/>
            </td>
        </tr>


    </xsl:template>

    <xsl:template name="collapsedcomment">
        <xsl:param name="comment"/>
        <tr class="toggle" id="collapsedcomment{$comment/id}">
            <td colspan="3">
                This comment by <em>
                    <xsl:value-of select="umbraco.library:GetMemberName($comment/memberId)"/>
                </em>
                has a very low score and been hidden, <a class="forumToggleComment" rel="comment{$comment/id}" href="#">Show it anyway</a>
            </td>
        </tr>


    </xsl:template>



    <xsl:template name="badge">
        <xsl:param name="id"/>
        <xsl:param name="date"/>
        <xsl:variable name="mem" select="umbraco.library:GetMember($id)"/>

        <div class="memberBadge">
            <span class="date">
                <abbr class="updated" title="{$date}">
                    <xsl:value-of select="umbraco.library:ShortDate($date, true(), ' ')"/>
                </abbr>
            </span>

            <span class="fn">
                    <xsl:value-of select="$mem/@nodeName"/>
            </span>

        </div>
    </xsl:template>

</xsl:stylesheet>